<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array(
		'0'=>'待付款_NO pay',
		'1'=>'待发货_NO send',
		'2'=>'待收货_NO get',
		'3'=>'已完成_Payment complete',
		'4'=>'已取消_exchange',
		'5'=>'申请退款',
		'6'=>'已退款',
		'7'=>'异常报备',
		'10'=>'拼单中',
		'11'=>'待售后接单',
		'12'=>'售后已接单',
		'13'=>'售后上门',
		'14'=>'售后异常',
		'15'=>'待售后',
		'16'=>'售后完成',
		'21'=>'部分发货',
		'22'=>'审批中',
		'-1'=>'已关闭',
);
$return['send_status']=$this->send_status;
$return['status2']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['paytype']['offpay']='线下收款';
$return['paytype']['offsao']='线下扫码';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
$install = check_account_permission('jy_peisong');
if($install){
	model('jy_peisong/order')->order_checktime();
}
$shopset=$this->plugin_setting('shopset');
if($_RQ['op']=='list'){
	//执行任务
	$this->model('task')->orderup(true);
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
    $orderby='createtime desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status'])){
		if($_RQ['status']==15){
			$params['status']=array(11,12,13,14,15,16);
		}elseif($_RQ['status']==1){
			$params['status']=array(1,21);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['starttime'] || $_RQ['endtime']){
		if($_RQ['starttime']){
			$return['params']['starttime']=$_RQ['starttime'];
		}
		if($_RQ['endtime']){
			$return['params']['endtime']=$_RQ['endtime'];
		}
		$_RQ['starttime']=$_RQ['starttime']?strtotime($_RQ['starttime']):0;
		$_RQ['endtime']=$_RQ['endtime']?strtotime($_RQ['endtime']):time();
		$params['createtime between']=array($_RQ['starttime'],$_RQ['endtime']);
	}
	$tip='所有订单显示，列表商品只显示第一个！';
	$tip .='</br><red>【注意】：本场景已开启独立商品展示，只显示本场景下的订单！</red>';
	$tip .='</br>【<red>订单时间显示不准确？</red>】：可能服务器的时间不准确，联系管理员同步一下服务器网络时间！';
	$tip .='</br>【<red>不显示发货按钮？</red>】：如果是商户的订单！只能商户自己发货，除非商户申请平台代发！';
	$tip .='</br>【<red>导出的订单是按下单时间？</red>】：因为有些订单未支付，不存在支付时间，只能按下单时间来导出！';
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
	}else{
			//$params['plugin_name']='';
	}
	if(!$params['plugin_name']){
		$params['plugin_name <>']='jy_newcar';
	}
	if($_RQ['status']==1){
		$batch=array(
			array('type'=>'href','url'=>'jy_weishop/order.hefa/','title'=>'合并发货'),
		);
	}
	$return['list']=$this->getslice('order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($this->plugin_exists('jy_shouhou') && $shopset['is_shouhou']){
		$jy_shouhou=1;
	}
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
		$item['member']['leveltitle']=pdo_getcolumn('jy_weishop_member_level',array('id'=>$levelid,'uniacid'=>$_SESSION['uniacid']),'title');
		$item['member']['leveltitle']=$item['member']['leveltitle']?:'普通会员';
		$goods=pdo_get('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),array('optionid','goodssn','productsn','optiondata','goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total']?:1;
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		$goods['goodssn']=$goods['goodssn']?:$goodsinfo['goodssn'];
		$goodsinfo['unit']=$goodsinfo['unit']?:'件';
		$goods['unit']=$goods['unit']?:$goodsinfo['unit'];
		$item['optioninfo']=$goods['optiondata'].' '.$goods['goodssn'].' x'.$goods['total'].'/'.$goods['unit'];
		if(!$item['merchid'] && $item['storeid']){
			$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
		}
		if($item['merchid']){
			$item['storetitle']='商户';
		}
		$item['storetitle']=$item['storetitle']?:'平台自营';
		$item['expressid']=pdo_getcolumn('jy_weishop_order_express',array('orderid'=>$item['id']),'id');
		$item['isjy_shouhou']=$jy_shouhou;
		//获取地址
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && ($item['address']['name'] || $item['address']['mobile'])){	
			$address=$item['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['addressid']));
		}
		$item['address']=model('core/member')->get_address_str($address);
		if(($item['sendtype']!=1 && $address['name']) || !$item['realname']){
			$item['realname']=$address['name'];
		}
		if($item['sendtype']==1 && $item['is_daifa']==1){
			$item['sendtype']='daifa';
		}
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['can_refund']=$this->model('order/refund')->check_order_rerefund($item['id']);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
			array('op'=>'add','title'=>'生成订单_Order info'),
		),
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/','tip'=>$tip,'edit'=>false,'total'=>$total,'psize'=>$psize,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'realname','title'=>'联系人_Realname','type'=>'string'),
			array('type'=>'string_more','title'=>'门店/商户_Store',
			'data'=>array(
				array('title'=>'','name'=>'storetitle'),
				array('title'=>'','name'=>'merchid','type'=>'select','options'=>'merchs')
				)
			),
			array('type'=>'img_string','title'=>'商品名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'','name'=>'optioninfo')
				)
			),
			array('type'=>'string_more','title'=>'金额/时间_Price',
			'data'=>array(
				array('title'=>'￥','name'=>'price'),
				array('title'=>'','name'=>'createtime')
				)
			),
			array('type'=>'string_more','title'=>'支付/状态_Is pay',
			'data'=>array(
				array('title'=>'','name'=>'ispay','type'=>'radio','options'=>'ispay'),
				array('title'=>'','name'=>'paytype','type'=>'radio','options'=>'paytype'),
				array('title'=>'','name'=>'buytype','type'=>'radio','options'=>'buytype')
				)
			),
		),
		'action'=>array(
			array('type'=>'modal','action'=>'/order.list/setstatus/','title'=>'设置状态_Set status'),
			array('type'=>'href','action'=>'/order.detail/','title'=>'订单详情_Detail'),
			array('type'=>'modal','action'=>'/order.detail/upprice/','title'=>'订单改价','show'=>array('status'=>0)),
			array('type'=>'href','action'=>'/order.fachange/','title'=>'发货记录','show'=>array('status'=>2,'expressid >'=>0)),
			//array('action'=>'/order.express//nomodal=1','title'=>'查看物流_Express','type'=>'href','show'=>array('status'=>2,'expressid >'=>0)),
			array('op'=>'fa','title'=>'发货_Ship','type'=>'href','show'=>array('merchid'=>0,'status'=>array(1,21),'sendtype'=>array(1,4),'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'代发货_Ship','type'=>'href','show'=>array('status'=>array(1,21),'sendtype'=>'daifa','send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'自提_Ship','type'=>'href','show'=>array('status'=>array(1,21),'sendtype'=>2,'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'虚拟发货_Ship','type'=>'href','show'=>array('status'=>array(1,21),'sendtype'=>3,'send_status'=>array(0,11))),
			//array('op'=>'peisong','title'=>'指定配送_Distribution','type'=>'modal','show'=>array('status'=>1,'isdish'=>1)),
			array('op'=>'print','title'=>'打印_Print','type'=>'post','show'=>array('merchid'=>0)),
			array('type'=>'post','op'=>'shou','title'=>'确认收货_Confirm','show'=>array('status'=>2)),
			array('type'=>'post','op'=>'refund','message'=>'确定要《发起退款》吗？','title'=>'发起退款_Refund','show'=>array('can_refund'=>true)),
			array('op'=>'shouhou','title'=>'发起售后_Ship','type'=>'modal','show'=>array('status'=>array(2,3),'isjy_shouhou'=>1)),			
		),'batch'=>$batch,
		),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'status','width'=>9,'title'=>'订单状态_Status','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>9,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>9,'title'=>'配送方式_Ship type','type'=>'radio','options'=>'sendtype'),
			array('type'=>'choose','width'=>11,'title'=>'所属会员ID','name'=>'mid','action'=>'core/member.get_member/'),
		)
	);
	if($params['status']==1){
		$htmls['list']['list'][]=array('name'=>'send_status','title'=>'配送状态','type'=>'radio','options'=>'send_status');
	}else{
		$htmls['list']['list'][]=array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status');
	}
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
		$htmls['search'][]=array('type'=>'choose','width'=>11,'title'=>'所属商户ID','name'=>'merchid','action'=>'jy_merch/get_merch/');
	}else{
		$merchs=array();
	}
	$htmls['search'][]=array('name'=>'starttime','width'=>10,'title'=>'开始时间','type'=>'time');
	$htmls['search'][]=array('name'=>'endtime','width'=>10,'title'=>'结束时间','type'=>'time');
	$merchs[0]=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	foreach($merchs as &$mv){
		if($mv['id']){
			$mv['title']='['.$mv['id'].'] '.$mv['merchname'];
		}
	}
	$return['merchs']=$merchs;
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$shopset=$this->plugin_setting('shopset');
	set_html('title',$shopset['title']);
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
    if(!empty($_RQ['ordersn'])){
        $params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
        $return['params']['ordersn']=$_RQ['ordersn'];
    }
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['starttime'] || $_RQ['endtime']){
		$_RQ['starttime']=$_RQ['starttime']?strtotime($_RQ['starttime']):0;
		$_RQ['endtime']=$_RQ['endtime']?strtotime($_RQ['endtime']):time();
		$params['createtime between']=array($_RQ['starttime'],$_RQ['endtime']);
		$return['params']['createtime']=$time_range;
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$fields=array('id','mid','ordersn','level_de_price','delcredit1','enough_de_price','coupon_de_price','luck_de_price','out_upfee','isdiscountprice','price','dispatchprice','merchid','address','addressid','remark','mobile','realname','status','paytype','ispay','createtime','paytime','sendtime','finishtime','storeid','diyformdata');
	$order=pdo_getall('jy_weishop_order',$params);
	if(count($order)>1000){
		exi('订单数量大于1000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$ordergoods=array();
	$jy_supplier=$this->plugin_exists('jy_supplier');
	foreach($order as &$v){
		$member=pdo_get('core_members',array('id'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','mobile'));
		$v['nickname']=$member['nickname'];
		$v['usermobile']=$member['mobile'];	
		$v['address']=json_decode($v['address'],true);
		if($v['address'] && ($v['address']['name'] || $v['address']['mobile'])){	
			$address=$v['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['addressid']));
		}
		$v['address']=model('core/member')->get_address_str($address);
		$v['address']=str_replace(',','，',$v['address']);
		$v['address']=str_replace("\n","，",$v['address']);
		$v['remark']=str_replace(',','，',$v['remark']);
		$v['remark']=str_replace("\n","，",$v['remark']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
		if($v['storeid']){
			$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['storeid']),'title');
		}
		$v['storetitle']=$v['storetitle']?:'无';
		$v['diyformdata']=json_decode($v['diyformdata'],true);
		$v['data2']='';
		if(is_array($v['diyformdata'])){
			foreach($v['diyformdata'] as $dk=>&$diyv){
					$v['data2'] .='【'.$diyv['title']."】：";
					if(is_array($diyv['data']) && $diyv['type']!=7){
						$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
					}
					if($diyv['type']==7){
						$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
					}
					$v['data2'] .=$diyv['data'];
			}
		}
		$invoice=pdo_get('jy_weishop_order_invoice',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$v['id']));
		if($invoice){
			$type=$invoice['type']==2?'增值税专用发票':'电子普通发票';
			$v['invoice']='['.$type.'] 抬头：'.$invoice['name'].' ，税号：'.$invoice['code'].' ，开户行：'.$invoice['bank'].' ，银行账号：'.$invoice['bankcode'].' ，邮箱：'.$invoice['email'];
		}
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','total'));
		foreach($goods as $k=>$gv){
			$info=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('goodssn','productsn','api_id','api_pluginname','title'));
			if($info['api_pluginname']=='jy_supplier'){
                $info['supplier_id']=$info['api_id'];
            }
            $goodstitle=$info['title'];
			//商品规格
			if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid'],'uniacid'=>$_SESSION['uniacid']),array('title','specs','goodssn','productsn'));
				$specs=$options['specs'];
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec,'uniacid'=>$_SESSION['uniacid']),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$options=array();
				$selespename='无规格';
			}
			$num=$k+1;
			if($k){
				$v=array();
			}
			$v['goodstitle']='【商品'.$num.'】'.$goodstitle;
			$v['selespename']=$selespename;
			$v['goodssn']=$options['goodssn']?:$info['goodssn'];
			$v['productsn']=$options['productsn']?:$info['productsn'];
			$v['total']=$gv['total'];
			if($jy_supplier && $info['supplier_id']){
				$v['supplier']=pdo_getcolumn('jy_supplier_user',array('id'=>$info['supplier_id'],'uniacid'=>$_SESSION['uniacid']),'merchname');
			}
            $v['costprice']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'costprice');
			$ordergoods[]=$v;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'storetitle'=>'所属门店',
		'goodstitle'=>'商品名称',
		'selespename'=>'规格',
		'goodssn'=>'编码',
		'productsn'=>'国家商品条码',
		'total'=>'数量',
		'nickname'=>'买家昵称',
		'usermobile'=>'买家电话',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'paytime'=>'支付时间',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
		'invoice'=>'发票信息',
        'costprice'=>'成本价',
        'supplier'=>'供货商',
		'remark'=>'备注',
		'data2'=>'表单内容',
		'dispatchprice'=>'运费',
		'level_de_price'=>'会员折扣',
		'delcredit1'=>'积分抵扣',
		'enough_de_price'=>'满额立减',
		'coupon_de_price'=>'优惠券优惠',
		'luck_de_price'=>'运势优惠',
		'isdiscountprice'=>'促销优惠',
		'out_upfee'=>'打包费',
	);
	cfc('tools')->export2excel($header,$ordergoods,'商城订单列表');
}
if($_RQ['op']=='setfa'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择订单','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$sendtype=pdo_getcolumn('jy_weishop_order',array('id'=>$id),'sendtype');
			if($sendtype==1){
				$this->model('order')->order_send($id);
			}else{
				$this->model('order')->order_send_virtual($id);
			}
		}
	}	
	exi('一键发货成功！');
}
if($_RQ['op']=='fa'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['canfa']){
			exi('[平台不能发货]：由商户或供货商自己发货！','error');
		}
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['status']==2){
			$isup=true;
		}
		if($post['sendtype']==1 || $post['sendtype']==4){
			if(!$post['morefa']){
				$post['goodsinfo']=$post['goods'];
			}
			if($post['express_type']==0){
				if($post['morefa'] && !count($post['goodsinfo'])){
					exi('请添加要分批发货的商品！','error');
				}
				if(!$post['fatype'] && (!$post['express'] || !$post['expresssn'])){
					exi('请填写快递信息','error');
				}
				if($post['fatype'] && !$post['content']){
					exi('请填写物流信息','error');
				}
				$this->model('order')->order_send($id,$post,$isup);
			}elseif($post['express_type']==1){//平台配送
				if(!$post['peisong_id'] && $post['need_setpeisong']){
					exi('请选择配送员！','error');
				}
				if(!$post['peisong_id'] && $post['set_peisonguser']==1){
					$post['peisong_id']=model('jy_peisong/order')->order_pai($post);
					$post['order_pai']=1;
				}
				if($post['peisong_id']){
					$post['order_id']=$id;
					if(!$post['order_pai']){
						model('jy_peisong/order')->order_sendget($post);
					}
					$this->model('express')->updata($post);
				}else{
					$post['send_status']=12;
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
					$this->model('order')->order_send($id,$post,$isup);
				}		
				exi('订单配送处理成功！','','jy_weishop/order.list//status=1');
			}elseif($post['express_type']==2){//商家自己配送
				$this->model('order')->order_send($id,'',$isup);
				exi('订单配送处理成功！','','jy_weishop/order.list//status=1');
			}elseif($post['express_type']==3){//达达配送
				$res=$this->model('order/dada_api')->dada_send($post);
				if($res['state']==1){
					$post['opensend_type']=1;
					$this->model('order')->order_send($id,$post,$isup);
				}else{
					exi($res['msg'],'error');
				}
			}elseif($post['express_type']==4){//货拉拉配送
				$res=model('jy_huolala/huo_api')->order_send($post);
				if($res['state']==1){
					$post['opensend_type']=2;
					$post['openorderid']=$res['openorderid'];
					$this->model('order')->order_send($id,$post,$isup);
				}else{
					exi($res['msg'],'error');
				}
			}		
		}elseif($post['sendtype']==3){
			$this->model('order')->order_send_virtual($id,$post['content']);
		}elseif($post['sendtype']==2){
			if($post['isverify']==2){
				exi('计次核销不能电脑端处理，请扫客户核销码处理！','error');
			}else{
				$this->model('verify')->self_verify($post['verifycode'],0,$post['morefa']);
			}
		}
		exi('已发货！','','/order.list/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','status','is_peisong','dispatchid','dispatchprice','sendtype','storeid','merchid','address','addressid','ordersn','remark','price','isdish','weight'));
	$params=array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']);
	$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('orderid','status','optionid','total','goodsid','istaocan','api_pluginname','api_id','id'));
	$item['canfa']=0;
	$return['ordergoods']=array();
	foreach($ordergoods as &$gv){
		$gv['sendtype']=$item['sendtype'];
		$gv['refund_id']=$this->pdo_getcolumn('order_refund',array('ordersn'=>$item['ordersn'],'ordergoodsid'=>$gv['id']),'id');
		if($gv['refund_id']){
			$gv['noedit']=1;
		}
		$gv['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']),'title');
		//商品规格
		if($gv['optionid']){
			$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
			if($gv['istaocan']){
				$gv['selespename']=$options['title'];
			}else{
				$specs=$options['specs'];
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$gv['selespename']=implode("，",$specs);
			}
		}else{
			$gv['selespename']='无';
		}
		$gv['istotal']=$this->model('express')->getistotal($gv);
		$gv['lstotal']=$gv['total']-$gv['istotal'];
		$gv['canfa']=$gv['lstotal']>0?1:0;
		if($gv['canfa']){
			$item['canfa']=1;
		}
		$return['ordergoods'][$gv['id']]=$gv;
		$return['ordergoods'][$gv['id']]['title']=$gv['title'].'['.$gv['selespename'].']';
	}
	$item['goods']=$ordergoods;
	$order_express=pdo_get('jy_weishop_order_express',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'morefa'=>0));
	if($order_express){
		$item['express']=$order_express['express_id'];
		$item['expresssn']=$order_express['expresssn'];
		$item['opensend_type']=$order_express['opensend_type'];
		$item['fatype']=$order_express['fatype'];
		$item['content']=$order_express['content'];
		$item['morefa']=$order_express['morefa'];
	}
	if($item['sendtype']==1 || $item['sendtype']==4){
		$htmls=array(
			'type'=>'edit',
			'tabs'=>array(
				array('op'=>'fa','title'=>'订单发货'),
				array('action'=>'/order.fachange//id='.$item['id'],'title'=>'发货记录'),
			),
		);
		$item['sendtitle']='运费'.$item['dispatchprice'].'元';
		if($item['dispatchid']){
			$sendtypetitle=pdo_getcolumn('jy_weishop_dispatch',array('id'=>$item['dispatchid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}else{
			$sendtypetitle='快递';
		}
		$citypeisong=$this->plugin_setting('citypeisong');
		$item['set_peisonguser']=$citypeisong['set_peisonguser'];
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
			if(!$item['isdish'] && $item['is_peisong']==2){
				$item['express_type']=0;
			}
			if($item['express_type']==3 && $citypeisong['hll_man'] && $item['weight']>=$citypeisong['hll_man']*1000){
				$item['express_type']=4;
			}
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}elseif($item['express_type']==4){
				$tip='商品订单已打包好,发送给货拉拉配送！(<red>【慎重操作】：确定要呼叫《货拉拉配送员》过来拿货吗?</red>)';
			}elseif(!$item['express_type']){
				$tip='商品订单已打包好,快递发货！';
				$item['sendtitle'] .='('.$sendtypetitle.')';
			}
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
			$item['sendtitle'] .='('.$sendtypetitle.')';
		}
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && ($item['address']['address'] || $item['address']['mobile'])){
			$address=$item['address'];
			$addressid=$item['address'];
		}else{
			$address=pdo_get('core_address',array('id'=>$item['addressid']));
			$addressid=$item['addressid'];
		}
		$item['address']=array(
			'address'=>model('core/member')->get_address_str($addressid),
			'mobile'=>$address['mobile'],
			'name'=>$address['name'],
		);
		$item['fatype']=0;
		$item['morefa']=0;
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'tip','title'=>'[平台不能发货]：由商户或供货商自己发货！','show'=>array('canfa'=>0)),
			array('type'=>'copy','title'=>'运费','name'=>'sendtitle'),
			array('type'=>'copy','title'=>'联系人','name'=>'address.name'),
			array('type'=>'copy','title'=>'联系电话','name'=>'address.mobile'),
			array('type'=>'copy','title'=>'收货地址','name'=>'address.address'),
			array('type'=>'array','name'=>'goods','title'=>'订单商品详情','noadd'=>1,'nodel'=>1,'nosplice'=>1,'noedit'=>1,'help'=>'[<red>无需发货</red>的商品]：一般是商户发货或者供货商发货！',
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'总量','name'=>'total','read'=>true),
					array('type'=>'string','title'=>'剩余量','name'=>'lstotal','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
					array('type'=>'radio','title'=>'是否需要发货','name'=>'canfa','options'=>'canfa'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
					array('type'=>'radio','title'=>'是否需要发货','name'=>'canfa','options'=>'canfa'),
				),
            ),
			array('type'=>'switch','title'=>'分批发货？','name'=>'morefa','show'=>array('express_type'=>0,'canfa'=>1)),
			array('type'=>'array','name'=>'goodsinfo','title'=>'要发货的商品','nosplice'=>1,'show'=>array('morefa'=>1),
                'list'=>array(
					array('type'=>'select','title'=>'商品名称+规格','name'=>'id','options'=>'ordergoods'),
					array('type'=>'string','title'=>'数量','name'=>'total'),
				),
				'details'=>array(
					array('type'=>'select','title'=>'商品名称+规格','name'=>'id','options'=>'ordergoods'),
					array('type'=>'string','title'=>'数量','name'=>'total'),
				),
            ),
			array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype','show'=>array('express_type'=>0,'canfa'=>1)),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0,'fatype'=>0,'canfa'=>1)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0,'fatype'=>0,'canfa'=>1)),
			array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('express_type'=>0,'canfa'=>1,'fatype'=>array(1,2))),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('express_type'=>1,'canfa'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1,'canfa'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！'),
		);
		$return['express']=$this->model('model')->express_types();
		$return['fatype']=array('快递配送','其他物流','无需物流');
	}else{
		$item['verifycode']=pdo_getcolumn('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),'number');
		$goods_verify=$this->pdo_getall('verifygoods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		if($goods_verify){
				foreach($goods_verify as &$gvv){
					$gvv['status']=0;
					if (!(empty($gvv['limitnum']))){
						$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$gvv['id'],'uniacid'=>$_SESSION['uniacid']));
						$verifynum = 0;
						foreach ($verifygoodlogs as &$verifygoodlog) 
						{
							$verifygoodlog['verifydate']=date('Y-m-d H:i:s',$verifygoodlog['verifydate']);
							$verifynum += intval($verifygoodlog['verifynum']);
						}
						$gvv['verifygoodlogs']=$verifygoodlogs;
						$gvv['lastverifys'] = intval($gvv['limitnum']) - $verifynum;
					}
					if (empty($gvv['limittype'])) {
							$gvv['limitdays']=$gvv['limitdays']?:365;
							$limitdate = intval($gvv['starttime']) + (intval($gvv['limitdays']) * 86400);
					}else{
							$limitdate = intval($gvv['limitdate']);
					}
					if ($limitdate < time()) {
						$gvv['status']=2;
					}
					if(!$gvv['lastverifys']){
						$gvv['status']=1;
					}
				}
		}
		$item['goods_verify']=$goods_verify;
		$item['code_endtime']=$code_endtime;
		if(count($return['virtual']) || count($item['goods_verify']) || count($item['order_verify'])){
			$item['isverify']=2;
		}else{
			$item['isverify']=1;
		}
		$htmls=array(
			'type'=>'edit',
			'desc'=>$item['sendtype']==3?'虚拟发货':'物品自提',
		);
		$item['morefa']=0;
		$htmls['edit']=array(
			//array('type'=>'switch','title'=>'分批发货？','name'=>'morefa'),
			array('type'=>'array','name'=>'goods','title'=>'商品详情','noadd'=>1,'nosplice'=>1,'noedit'=>1,'show'=>array('morefa'=>0),
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
					array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
				),
            ),
			array('type'=>'array','name'=>'goods','title'=>'商品详情','noadd'=>1,'nosplice'=>1,'editbtn'=>'商品发货','show'=>array('morefa'=>1),
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
					array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
				),
            ),
			array('type'=>'array','name'=>'goods_verify','title'=>'计次消费码','noadd'=>1,'nosplice'=>1,'noedit'=>1,'show'=>array('isverify'=>2,'morefa'=>0),
                'list'=>array(
					array('type'=>'string','title'=>'消费码','name'=>'verifycode','read'=>true),
					array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'verifystatus','read'=>true),
					array('type'=>'string','title'=>'剩余次数','name'=>'lastverifys'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
					array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
				),
            ),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('isverify'=>1,'sendtype'=>2,'morefa'=>0)),
			array('type'=>'text','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3,'morefa'=>0),'help'=>'虚拟物品发货，填写发货内容！'),
		);
	}
	$return['verifystatus']=array('待使用','已使用','已过期');
	$return['canfa']=array('无需发货','需要发货');
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
		$users=pdo_getall('jy_peisong_member',array('uniacid'=>$_SESSION['uniacid'],'group_id'=>$v['id'],'status'=>1),array('mid','title'));
		foreach($users as $uv){
			$peisongs[]=$uv;
		}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='changeexpress'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express_id'=>$post['express'],'expresssn'=>$post['expresssn']);
				$this->pdo_update('order_express',$data,array('orderid'=>$id));		
		}
		exi('修改物流信息成功！！','','/order.list/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','is_peisong','sendtype','storeid','merchid','addressid','ordersn','remark','price','isdish'));	
	$express=$this->pdo_get('order_express',array('orderid'=>$id));
	$item['express']=$express['express_id'];
	$item['expresssn']=$express['expresssn'];
	$item['fatype']=$express['fatype'];
	$item['content']=$express['content'];
	$htmls=array(
			'type'=>'edit',
			'desc'=>'修改物流',
	);
	$htmls['edit']=array(
			array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype'),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('fatype'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('fatype'=>0)),
			array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('fatype'=>1)),
	);
	$return['fatype']=array('快递配送','其他物流','无需物流');
	$return['express']=$this->model('model')->express_types();
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='setstatus'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$update=array('status'=>$status,'ispay'=>intval($post['ispay']));
		if($status==3){
			$update['finishtime']=time();
		}
		$this->model('task')->statuscheck($post,$status);
		pdo_update('jy_weishop_order',$update,array('id'=>$id));
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','ispay','ordersn','status','storeid','createtime'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'ordersn','name'=>'ordersn','read'=>true),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
			array('type'=>'radio','title'=>'是否支付','name'=>'ispay','options'=>'ispay'),
	);
	$return['status']=array(
		'0'=>'待付款',
		'1'=>'待发货',
		'2'=>'待收货',
		'3'=>'已完成',
		'4'=>'已取消',
		'7'=>'异常报备',
		'10'=>'拼单中',
		'11'=>'待售后接单',
		'12'=>'售后已接单',
		'13'=>'售后上门',
		'14'=>'售后异常',
		'15'=>'待售后',
		'16'=>'售后完成',
		'21'=>'部分发货',
		'22'=>'审批中',
		'-1'=>'已关闭',
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='peisong'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['peisong_id']){
			exi('请选择配送员！','error');
		}
		$params['order_id']=$id;
		$params['peisong_id']=$post['peisong_id'];
		model('jy_peisong/order')->order_get($params);		
		exi('指定配送员成功！','','jy_weishop/order.list//status=1');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype','storeid','merchid'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单指定配送',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','help'=>'只能指定该门店下面的配送员'),
	);
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
				$users=pdo_getall('jy_peisong_member',array('uniacid'=>$_SESSION['uniacid'],'group_id'=>$v['id'],'status'=>1),array('mid','title'));
				foreach($users as $uv){
					$peisongs[]=$uv;
				}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='shouhou'){
	$install = check_account_permission('jy_shouhou');
	if(!$install){
		exi('请联系管理员开通《售后上门》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['express_type']!=1){
				if(!$post['shouhou_id'] && $post['need_setpeisong']){
					exi('请选择售后员！','error');
				}
				if($post['shouhou_id']){
					$params['order_id']=$id;
					$params['shouhou_id']=$post['shouhou_id'];
					model('jy_shouhou/order')->order_sendget($params);
				}else{
					pdo_update('jy_weishop_order',array('status'=>15),array('id'=>$id));
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新售后订单！','shouhou_order');
				}			
				exi('订单售后处理成功！','','/order.list/list/status=15');
		}elseif($post['express_type']==1){
				//商家自己售后
				pdo_update('jy_weishop_order',array('status'=>15),array('id'=>$id));
				exi('订单售后处理成功！','','/order.list/list/status=15');
		}
		exi('已发起售后！','','/order.list/list/status=15');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype','storeid','merchid','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
			'type'=>'edit',
			'desc'=>'发起售后',
	);
	$tip='客户已收到商户,需要售后上门服务！';
	$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定售后员？','help'=>'不指定，则由售后员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定售后员','name'=>'shouhou_id','options'=>'shouhous','option_value'=>'mid','help'=>'只能指定该门店下面的售后员，或本城市的平台售后员！'),
	);
	$return['express']=$this->model('model')->express_types();
	$shouhous=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_shouhou_area',array('store_id'=>0));
	}else{
		$areas=pdo_getall('jy_shouhou_area',array('store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
			$users=pdo_getall('jy_shouhou_member',array('group_id'=>$v['id'],'status'=>1),array('mid','title'));
			foreach($users as $uv){
					$shouhous[]=$uv;
			}
	}
	$return['shouhous']=$shouhous;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	//exi('暂时不支持，后台手动收货！','error');
	$this->model('order')->order_receive($id);
	exi('已收货','','//list/status=3');
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id),array('ispay'));
	$print_type=18;
	$res=$this->model('printmsg')->storeprint('',$order_id,'','','','','','',$print_type,array('is18'=>1));
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}

if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>1);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'order.detail','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['goodsid'] || (!$post['mid'] && $post['is_new']!=1)){
			exi('必须选择商品，会员等','error');
		}
		$mid=$post['mid'];
		if($post['is_new']==1){
			$member=array(
				'nickname'=>trim($post['name']),
				'mobile'=>trim($post['mobile']),
				'uniacid'=>$_SESSION['uniacid'],
				'username'=>'user_'.random(10),
				'from_os'=>get_os(),
				'is_virtual'=>1,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('core_members',$member);
			$mid=pdo_insertid();
		}
		if($post['is_en']==1){
			$post['pcd']=array(
				'countries'=>$post['countries'],
				'province'=>$post['province'],
				'city'=>$post['city'],
				'district'=>$post['district'],
			);
		}
		$data=array(
			'isdefault'=>1,
			'mobile'=>trim($post['mobile']),
			'name'=>trim($post['name']),
			'pcd'=>ijson_encode($post['pcd']),
			'address'=>trim($post['address']),
			'area_code'=>trim($post['area_code'])
		);
		$data['mid']=$mid;
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('core_address',$data);
		$addressid=pdo_insertid();
		$goods=pdo_get('jy_weishop_goods',array('id'=>$post['goodsid']));
		$goods['total']=$post['total']?:1;
		$goods['goodsid']=$goods['id'];
		if($post['createtime']){
			$mins=rand(1,59);
			$createtime=strtotime($post['createtime'])+$mins;
			if($post['isrand']){
				$createtime +=$mins*318;
			}
		}elseif($post['isrand']){
			$rand=rand(2,30);
			$days=rand(8,30);
			$createtime=TIMESTAMP-$days*24*3600-$rand*600;
		}else{
			$createtime=TIMESTAMP;
		}
		do{
			$nonce = random(6,true);
			$ordersn=date('YmdHis',$createtime).$nonce;
		}while(pdo_getcolumn('jy_weishop_order',array('ordersn'=>$ordersn),'ordersn'));
		$new_order=array(
			'mid'=>$mid,
			'price'=>$goods['price'],
			'goodsprice'=>$goods['price'],
			'goods'=>array($goods),
			'addressid'=>$addressid,
			'sendtype'=>1,
			'type'=>1,
			'createtime'=>$createtime,
			'ordersn'=>$ordersn,
		);
		$id=$this->model('order')->create($new_order);
		if($post['isrand'] || $post['createtime']){
			$update=array('status'=>3,'ispay'=>intval($post['ispay']));
			$rand=rand(2,50);
			if($post['createtime']){
				$days=rand(3,6);
				$mins=rand(1,59)*2;
				$update['finishtime']=$createtime+$days*24*3600+$rand*600+$mins;
			}else{
				$days=$days-3;
				$update['finishtime']=TIMESTAMP-$days*24*3600-$rand*600;
			}
			if($update['finishtime']<TIMESTAMP){
				pdo_update('jy_weishop_order',$update,array('id'=>$id));
			}
		}
		exi('生成订单成功！');
	}
	$item=array(
		'countries'=>'US',
		'province'=>'',
		'city'=>'',
		'is_new'=>0,
		'is_en'=>0,
	);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'生成订单',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
			array('op'=>'add','title'=>'生成订单_Order info'),
		),
		'edit'=>array(
			array('title'=>'为了便于真实性，所有生成的订单时间都是两天前的','type'=>'tip'),
			array('name'=>'isrand','title'=>'订单时间随机？','type'=>'switch'),
			array('name'=>'createtime','title'=>'指定创建时间','type'=>'time'),
			array('name'=>'is_new','title'=>'新会员？','type'=>'switch'),
			array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'core/member.get_member//is_virtual=1','choose_title'=>'选择会员','show'=>array('is_new'=>0),'help'=>'这里只能选择虚拟会员！不会显示真实会员！'),
			array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
			array('name'=>'total','title'=>'商品数量','type'=>'string'),
			array('name'=>'name','title'=>'收件姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'收件电话','type'=>'string'),
			array('name'=>'is_en','title'=>'外国地址？','type'=>'switch'),
			array('name'=>'pcd','title'=>'区域地址','type'=>'pcd','show'=>array('is_en'=>0)),
			array('name'=>'countries','title'=>'国家','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'province','title'=>'州','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'city','title'=>'城市','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'district','title'=>'区域','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'area_code','title'=>'邮编','type'=>'string','show'=>array('is_en'=>1)),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='refund'){
	$id=intval($_RQ['id']);
	if($id){
		$order=pdo_get('jy_weishop_order',array('id'=>$id));	
	}else{
		exi('请选择订单！','error');
	}
	$params=array(
			'type'=>0,
			'orderid'=>intval($order['id']),
			'price'=>$order['price'],
			'content'=>'管理员后台手动替客户申请！',
	);
	$this->model('order/refund')->refund_create($params);
	exi('申请成功！请耐心等待财务审核！','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));
			}
		}
	}
	exi('删除成功！','','list');
}
if($_RQ['op']=='ddel'){
	$this->check_candel('jy_weishop');
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！','','deleted');
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($_RQ['merchid']),
		'api_id'=>intval($_RQ['api_id']),
		'api_pluginname'=>$_RQ['api_pluginname']?:'',
	);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
?>